#include "PrinterPdf.h"

PrinterPdf::PrinterPdf(QObject *parent) : QObject(parent), ready(false), width_(595), height_(842), fieldSz_(10)
{
    outFileName = "default";
}

void PrinterPdf::setWidth(int width)
{
    width_ = width;
}

void PrinterPdf::setHeight(int height)
{
    height_ = height;
}

int PrinterPdf::getWidth()
{
    return width_;
}

int PrinterPdf::getHeight()
{
    return height_;
}

void PrinterPdf::setFieldSz(int fieldSz)
{
    fieldSz_ = fieldSz;
}

int PrinterPdf::getFieldSz()
{
    return fieldSz_;
}

void PrinterPdf::setInputFile(QString csvFile)
{
    inFileRoot  = csvFile.toStdString();
    ready       = true;
}

void PrinterPdf::setOutputFile(QString pdfName)
{
    outFileName = pdfName.toStdString();
}

bool PrinterPdf::print()
{
    // print
    string  printer     = "PDF";
    ///
    if (ready)  drawTempData();
    else        return false;
    ///
    int job_id = cupsPrintFile(printer.c_str(), tmpfilename, outFileName.c_str(), 0, NULL);
    unlink(tmpfilename);
    if (job_id != 0)    return true;
    else                return false;
}

void PrinterPdf::printing()
{
    bool b = print();
    if (!b) cout << "wrong print !!!" << endl;
    else    emit printed();
}

string PrinterPdf::createRow(string row, string delimiter)
{
    istringstream ss(row);
    string token;
    string field = "";
    while(std::getline(ss, token, ',')) {
        //        std::cout << token << '\n';
        field += delimiter + createField(token);
    }
    field += delimiter;
    //    cout << field;
    //    cout << endl;
    return field;
}

string PrinterPdf::createField(string fieldStr)
{
    size_t fieldSz  = fieldSz_;
    string field = string(fieldSz, ' ');
    //    cout << "|" << field << "|" << endl;
    //    string num = "12";
    field.replace(0, fieldStr.size(), fieldStr);
    //    cout << "|" << header << "|" << endl;
    return field;
}

vector<string>  PrinterPdf::prepareTempData()
{
    string      mas = "+";
    string      bar = "|";
    ifstream    file(inFileRoot.c_str());
    string      temp;
    string      s = "";
    vector<string> rows;
    s = "----------,----------,----------,----------";
    rows.push_back(createRow(s, mas));
    s = "Mesa,Rentas,Minutos,S/.";
    rows.push_back(createRow(s, bar));
    s = "----------,----------,----------,----------";
    rows.push_back(createRow(s, mas));
    while(std::getline(file, temp)) {
        s = createRow(temp, bar);
//        cout << s << endl;
        rows.push_back(s);
    }
    file.close();
    s = "----------,----------,----------,----------";
    rows.push_back(createRow(s, mas));
    return rows;
}

void PrinterPdf::drawTempData()
{
    // setup
//    tmpfilename = tempnam(NULL,NULL); // in menory
    tmpfilename= "/tmp/pdf-cairo.tmp";  // in hard disk
    cairo_surface_t* surface = cairo_ps_surface_create(tmpfilename, width_, height_);
    cairo_t *context = cairo_create(surface);

    // draw some text
    string  font    = "Monospace";
    cairo_select_font_face(context,
                           font.c_str(),
                           CAIRO_FONT_SLANT_NORMAL,
                           CAIRO_FONT_WEIGHT_NORMAL);
    double  szFont  = 20;
    cairo_set_font_size(context, szFont);
    double  x0  = 20;
    double  y0  = 80;
    vector<string> rows;
    rows = prepareTempData();
    for (size_t i = 0; i < rows.size(); i++)
    {
        cairo_move_to(context, x0, y0 + i*szFont);
        cairo_show_text(context, rows.at(i).c_str());   // the text we got as a parameter
    }
    // finish up
    cairo_show_page(context);
    cairo_destroy(context);
    cairo_surface_flush(surface);
    cairo_surface_destroy(surface);
}
